package com.blensmile.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.blensmile.R;
import com.blensmile.utils.DensityUtils;


/**
 * Created by Blensmile on 2016/11/14.
 */
public class DrawView extends View {

    private ClickCallBack callBack;
    private Point point = new Point();
    private Point pointStart = new Point();
    private int mWidth;//直径
    private int rOuter;//外部圆圈半径
    private int rInner;//内部圆圈半径
    private int radiusGap ;

    public DrawView(Context context) {
        this(context,null);
    }

    public DrawView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getWidth();
        rOuter = mWidth/2;
        rInner = DensityUtils.dip2px(getContext(), 65);
        radiusGap = rOuter - rInner;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 创建画笔
        Paint p = new Paint();
        p.setAntiAlias(true);
        //大圆圈
        p.setColor(Color.WHITE);
        p.setStyle(Paint.Style.FILL);
        RectF rec = new RectF(0, 0, mWidth, mWidth);
        canvas.drawArc(rec, 0, 360, false, p);

        //两条线
        int r = rOuter;
        p.setColor(0xff86c3c2);
        float xStart = (float) (r * Math.cos(Math.PI / 4));
        float lefted = r - xStart;
        canvas.drawLine(lefted, lefted, mWidth - lefted, mWidth - lefted, p);
        canvas.drawLine(lefted, mWidth - lefted, mWidth - lefted, lefted, p);

        //计算点击的范围

        int startange = 0;
        if (Math.sqrt(point.x * point.x + point.y * point.y) > rInner) {
            if (point.x > point.y) {
                startange =point.x > Math.abs(point.y)? 315:225;
            } else if (point.x < point.y) {
                startange = Math.abs(point.x) > Math.abs(point.y) ? 135:45;
            } else if (point.x == 0 && point.y == 0) {
                startange = 0;
            }
        } else {
            //内部圆圈
        }
        //设置点击的背景色
        p.setStyle(Paint.Style.FILL);
        if (startange != 0) {
            p.setColor(0xffe5e5e5);
            rec.set(0, 0, mWidth, mWidth);
            canvas.drawArc(rec, startange, 90, true, p);
        }
        //画内部圆圈
        p.setColor(0xff86c3c2);
        rec.set(rOuter - rInner, rOuter - rInner, rOuter + rInner, rOuter + rInner);
        canvas.drawArc(rec, 0, 360, false, p);
        //中间的图标
        Drawable drawable = getContext().getResources().getDrawable(R.drawable.learn_play);
        int iconWidth = DensityUtils.dip2px(getContext(), 54)/2;
        int iconHeight = drawable.getIntrinsicHeight() * iconWidth / drawable.getIntrinsicWidth();
        int marginLeft = DensityUtils.dip2px(getContext(), 5);
        drawable.setBounds(rOuter - iconWidth + marginLeft, rOuter - iconHeight, rOuter + iconWidth + marginLeft, rOuter + iconHeight);
        drawable.draw(canvas);

        drawable = getContext().getResources().getDrawable(R.drawable.learn_setting);
        iconWidth = DensityUtils.dip2px(getContext(), 26)/2;
        iconHeight = drawable.getIntrinsicHeight() * iconWidth / ( 2 * drawable.getIntrinsicWidth());
        drawable.setBounds(radiusGap - iconWidth, rOuter - iconHeight, radiusGap + iconWidth, rOuter + iconHeight);
        drawable.draw(canvas);

        drawable = getContext().getResources().getDrawable(R.drawable.learn_exit);
        iconHeight = drawable.getIntrinsicHeight() * iconWidth / (2 / drawable.getIntrinsicWidth());
        drawable.setBounds(rOuter - iconWidth, radiusGap - iconHeight, rOuter + iconWidth, radiusGap + iconHeight);
        drawable.draw(canvas);

        drawable = getContext().getResources().getDrawable(R.drawable.learn_report);
        iconHeight = drawable.getIntrinsicHeight() * iconWidth / (2 * drawable.getIntrinsicWidth());
        drawable.setBounds(mWidth - radiusGap - iconWidth, rOuter - iconHeight, mWidth - radiusGap + iconWidth, rOuter + iconHeight);
        drawable.draw(canvas);

        drawable = getContext().getResources().getDrawable(R.drawable.learn_relearn);
        iconHeight = drawable.getIntrinsicHeight() * iconWidth / (2 * drawable.getIntrinsicWidth());
        drawable.setBounds(rOuter - iconWidth, mWidth - radiusGap - iconHeight, rOuter + iconWidth, mWidth - radiusGap + iconHeight);
        drawable.draw(canvas);
        p.setColor(Color.BLACK);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                point.x = (int) event.getX() - rOuter;
                point.y = (int) event.getY() - rOuter;
                pointStart.x = point.x;
                pointStart.y = point.y;
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
//                point.x = (int) event.getX() - rOuter;
//                point.y = (int) event.getY() - rOuter;
//                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                pointStart.x = (int) event.getX() - rOuter;
                pointStart.y = (int) event.getY() - rOuter;
                checkCallBack();
            case MotionEvent.ACTION_CANCEL:
                point.x = 0;
                point.y = 0;
                invalidate();
                break;
        }
        return true;
    }

    private void checkCallBack(){
        if(callBack != null ){
            if (Math.sqrt(pointStart.x * pointStart.x + pointStart.y * pointStart.y) > rOuter) {
                //大圆外面，相当于取消

            } else if (Math.sqrt(point.x * point.x + point.y * point.y) > rInner) {
                if (point.x > point.y) {
                    if (point.x > Math.abs(point.y)) {
                        callBack.onReportClick();
                    } else {
                        callBack.onExitClick();
                    }
                } else if (point.x < point.y) {
                    if (Math.abs(point.x) > Math.abs(point.y)) {
                        callBack.onSettingClick();
                    } else {
                        callBack.onRelearnClick();
                    }
                }
            } else {
                //内部圆圈
                callBack.onPlayClick();
            }
        }
    }

    public void setCallBack(ClickCallBack callBack) {
        this.callBack = callBack;
    }

    public interface ClickCallBack{
        void onPlayClick();
        void onSettingClick();
        void onExitClick();
        void onReportClick();
        void onRelearnClick();
    }
}

